from django.shortcuts import render
from django.shortcuts import redirect
from . import models
from . import forms
import hashlib
import datetime
from django.conf import settings
from django.core.mail import EmailMultiAlternatives


def send_email(email, code):
    subject = '来自kyrie的注册确认邮件'
    text_content = '感谢注册www.xialeixue.github.io，这里是kyrie的个人主页和博客，专注于Python、Django和数据挖掘技术的分享！如果你看到这条消息，说明你的邮箱服务器不提供HTML链接功能，请联系管理员！'
    html_content = '''
                    <p>感谢注册，这里是kyrie的个人主页和博客，专注于Python、Django和数据挖掘技术的分享！</p>
	                <p>请点击以下站点链接完成注册确认！此链接有效期是7天</p>
	                <a href="http://{}/confirm/?code={}">点击完成注册</a>
                    '''.format('127.0.0.1:8000', code, settings.CONFIRM_DAYS)
    msg = EmailMultiAlternatives(subject, text_content, settings.EMAIL_HOST_USER, [email])
    msg.attach_alternative(html_content, 'text/html')
    msg.send()


def make_confirm_strin(user):
    now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    code = hash_code(user.name, now)
    models.ConfirmString.objects.create(code=code, user=user, )
    return code


def hash_code(s, salt='fudan2017'):
    h = hashlib.sha256()
    s += salt
    h.update(s.encode())
    return h.hexdigest()


# 手写表单后端
# def login(request):
#     if request.method == "POST":
#         username = request.POST.get('username')
#         password = request.POST.get('password')
#         message = '请检查输入内容'
#         if username.strip() and password:
#             try:
#                 user = models.User.objects.get(name=username)
#             except:
#                 message = '用户不存在'
#                 return render(request, 'login/login.html', {'message': message})
#
#             if user.password == password:
#                 print(username, password)
#                 return redirect('/index/')
#             else:
#                 message = '密码错误'
#                 return render(request, 'login/login.html', {'message': message})
#         else:
#             return render(request, 'login/login.html', {'message': message})
#     return render(request, 'login/login.html')


# django生成forms
def login(request):
    if request.session.get('is_login', None):
        return redirect('/index/')

    if request.method == "POST":
        login_form = forms.UserForm(request.POST)
        message = '请检查输入内容'
        if login_form.is_valid():
            username = login_form.cleaned_data.get('username')
            password = login_form.cleaned_data.get('password')

            try:
                user = models.User.objects.get(name=username)
            except:
                message = '用户不存在'
                return render(request, 'login/login.html', locals())

            if not user.has_confirmed:
                message = '该用户还未经过邮件确认'
                return render(request, 'login/login.html', locals())

            if user.password == hash_code(password):
                request.session['is_login'] = True
                request.session['user_id'] = user.id
                request.session['user_name'] = user.name
                return redirect('/index/')
            else:
                message = '密码错误'
                return render(request, 'login/login.html', locals())
        else:
            return render(request, 'login/login.html', locals())
    login_form = forms.UserForm()
    return render(request, 'login/login.html', locals())


def logout(request):
    if not request.session.get('is_login', None):
        return redirect('/login/')
    request.session.flush()
    # 或者使用下面的方法
    # del request.session['is_login']
    # del request.session['user_id']
    # del request.session['user_name']
    return redirect('/login/')


def index(request):
    if not request.session.get('is_login', None):
        return redirect('/login/')
    return render(request, 'login/index.html')


def register(request):
    if request.session.get('is_login', None):
        return redirect('/index/')

    if request.method == 'POST':
        register_form = forms.RegisterForm(request.POST)
        message = '请检查输入内容'
        if register_form.is_valid():
            username = register_form.cleaned_data.get('username')
            password1 = register_form.cleaned_data.get('password1')
            password2 = register_form.cleaned_data.get('password2')
            email = register_form.cleaned_data.get('email')
            sex = register_form.cleaned_data.get('sex')

            if password1 != password2:
                message = '两次输入密码不同'
                return render(request, 'login/register.html', locals())
            else:
                same_user_name = models.User.objects.filter(name=username)
                if same_user_name:
                    message = '该用户已经存在'
                    return render(request, 'login/register.html', locals())
                same_email_name = models.User.objects.filter(email=email)
                if same_email_name:
                    message = '该邮箱已被注册'
                    return render(request, 'login/register.html', locals())

                new_user = models.User()
                new_user.name = username
                new_user.password = hash_code(password1)
                new_user.email = email
                new_user.sex = sex
                new_user.save()

                code = make_confirm_strin(new_user)
                print('确认码：' + code)
                send_email(email, code)

                message = '请前往邮箱进行确认'
                return render(request, 'login/confirm.html', locals())

        else:
            return render(request, 'login/register.html', locals())

    register_form = forms.RegisterForm()
    return render(request, 'login/register.html', locals())


def confirm(request):
    code = request.GET.get('code', None)
    message = ''

    try:
        confirm = models.ConfirmString.objects.get(code=code)
    except:
        message = '无效的确认请求'
        return render(request, 'login/confirm.html', locals())

    c_time = confirm.c_time
    now = datetime.datetime.now()
    if now > c_time + datetime.timedelta(settings.CONFIRM_DAYS):
        confirm.user.delete()
        message = '您的邮件已过期！请重新注册'
        return render(request, 'login/confirm.html', locals())
    else:
        confirm.user.has_confirmed = True
        confirm.user.save()
        confirm.delete()
        message = '谢谢确认，请使用账户登录'
        return render(request, 'login/confirm.html', locals())
